package com.business.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.business.common.dto.R;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisUtil;
import com.business.user.annotation.TokenPermission;
import com.business.user.entity.User;
import com.business.user.entity.UserDataQuantity;
import com.business.user.entity.UserDetails;
import com.business.user.entity.UserDto;
import com.business.user.service.UserDataQuantityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户数据统计表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:28:26
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/dataQuantity")
public class UserDataQuantityController {


    @Autowired
    private UserDataQuantityService userDataQuantityServiceImpl;


    /**
     * 查询用户数据统计表
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryListByUser")
    @ResponseBody
    @TokenPermission
    public R queryByUser(UserDto user) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", user.getId());
        return queryListByUserId(jsonObject);
    }

    /**
     * 查询用户数据统计表
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryListByUserId")
    @ResponseBody
    public R queryListByUserId(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String key = String.format(RedisKeyConstant.USER_DATA_QUANTITY_QUERY_BY_USER, id);
        if (RedisUtil.hasKey(key)) {
            return R.success(RedisUtil.getValue(key));
        } else {
            UserDataQuantity r = userDataQuantityServiceImpl.queryByUser(id);
            RedisUtil.setValue(key, r, ExpiresTimeEnum.LEVEL_8.getExpiresTime());
            return R.success(r);
        }
    }


}